import json
from sweetest.globals import g
from os import path


def create(testsuite, htmlfile):
    '''
    创建html报告
    '''
    print('======' + json.dumps(testsuite))
    report_view = {}
    report_cases = []

    report_view['cases'] = report_cases
    report_view['browserName'] = g.browserName
    report_view['testTime'] = g.start_time
    report_view['endTime'] = g.end_time
    report_view['totalCount'] = 0
    report_view['successCount'] = 0
    report_view['title'] = '云南移动_UI自动化测试报告'

    for case in testsuite:
        if (case['result'] == 'Skip') | (case['result'] == 'Block') | (case['flag'] == 'N'):
            continue
        report_view['totalCount'] += 1
        report_case = dict()
        report_case['testData'] = ''
        report_case['finishTime'] = case.get('finish_time', '')
        report_case['caseName'] = case['id'] + ' ' + case['title']
        report_case['useTime'] = 0
        report_case['mark'] = case['remark']
        report_case['status'] = 'fail'
        if case['result'] == 'Pass':
            report_case['status'] = 'success'
            report_view['successCount'] += 1
        report_case['screenshot'] = []
        report_case['steps'] = []

        if len(case['steps']) > 0:
            for step in case['steps']:
                if (step['keyword'] != 'EXECUTE') & (step['score'] != '') & (step['score'] != '-'):
                    report_step = dict()
                    report_step['mark'] = ''
                    if step['keyword'] == 'CHECK':
                        report_step['mark'] += '预期结果：' + step.get('_expected_data', '') \
                            + '<br>实际结果：' + step.get('_real_data', '')

                    if step['keyword'] == 'INPUT':
                        report_step['mark'] += '输入内容：' + ('' if len(step['data']) < 1 else step['data']['text'])

                    if step['score'] == 'OK':
                        report_step['status'] = True
                    else:
                        report_step['status'] = False
                        report_step['mark'] += '<br><span style="color:red;">出错信息:' + step.get('remark', '') + '</span>'

                    report_step['stepId'] = case['id'] + str(step['no']).replace('*', '-').replace('>', '-').replace('<', '-').replace('^', '-')

                    report_step['stepName'] = step['_keyword'] + step['_element']
                    report_step['testTime'] = step.get('test_time', '')
                    if step.get('snapshot_path', '') != '':
                        report_step['screenshot'] = step['snapshot_path']
                    report_case['steps'].append(report_step)
        report_cases.append(report_case)

    file_object = open(path.join('./', 'webReport.xml'), encoding='utf-8')
    try:
        file_context = file_object.read()
        file_context = file_context.replace('#JSON_STRING__JSON_STRING#', json.dumps(report_view))
        with open(htmlfile, 'w', encoding='utf-8') as f:
            f.write(file_context)
    finally:
        file_object.close()




